<?php

namespace Admin\Controllers\Organizes;

use Admin\Controllers\BaseAdminController;
use Organize\Resources\LeaderResources;
use Illuminate\Http\Request;
use Platform\Models\Organize\Leader;
use Platform\Models\Organize\Organize;
use Platform\Repository\Organize\LeaderRepository;
use Platform\System\Tools\ResultTool;

/**
 *  组织成员管理
 * Class UserController
 * @package App\Http\Controllers\Admin\System
 */
class OrganizeLeaderController extends BaseAdminController
{
    /**
     * @var LeaderRepository
     */
    protected $leader_repository;

    /**
     * @var Organize
     */
    protected $organize;

    public function __construct(LeaderRepository $leaderRepository)
    {
        parent::__construct();
        $this->leader_repository = $leaderRepository;
    }

    /**
     *   成员列表
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $this->getOrganize();
        $leaders = $this->leader_repository->screen($request,$this->organize);
        return myResult()->setData($leaders,LeaderResources::class)->setPage($leaders)->send();
    }

    /**
     *  成员详情
     *
     * @param Leader $leader
     * @return mixed
     */
    public function show(Leader $leader){
        $leader->load(['role','organize']);
        return myResult()->setData($leader,LeaderResources::class)->send();
    }

    /**
     *  创建成员
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $this->getOrganize();
        $data = $this->verify($request);
        Leader::create($data);

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'新增成功')->send();
    }

    /**
     *  编辑成员
     *
     * @param Request $request
     * @param Leader $leader
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request, Leader $leader)
    {
        $this->getOrganize($leader->organize);
        $data = $this->verify($request,$leader);
        $leader->update($data);

        return myResult()->setCode(ResultTool::CODE_SUCCESS,'编辑成功')->send();
    }

    /**
     *  删除成员
     * @param Leader $leader
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function destroy(Leader $leader)
    {
        $this->getOrganize($leader->organize);

        if ($leader->isOrganizeAdmin())
            myResult()->stop(ResultTool::CODE_TARGET_WRONG,'默认账号不可删除');

        $leader->delete();
        return myResult()->setCode(ResultTool::CODE_SUCCESS,'删除成功')->send();
    }

    /**
     * 数据验证
     * @param Request $request
     * @param Leader $leader
     * @return array
     */
    public function verify(Request $request,Leader $leader = null){

        $data = $request->only(['username','password','name','avatar','role']);

        if ($leader) {

            if (isset($data['username'])){
                $admin_log = $this->leader_repository->getOneByUsernameExpectThis($data['username'],$leader->id);
                if ($admin_log)
                    myResult()->stop(ResultTool::CODE_TARGET_WRONG,'用户名已被使用');
            }

            if ($leader->isOrganizeAdmin()){
                unset($data['role']);
            }

        }else{

            if (empty($data['username']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST, '用户名不可为空');

            if (getStrLength($data['username']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'用户名过长');

            $admin_log = $this->leader_repository->getOneByUsername($data['username']);
            if ($admin_log)
                myResult()->stop(ResultTool::CODE_TARGET_WRONG,'用户名已被使用');

            if (empty($data['password']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST,'密码不可为空');

            $data['password'] = bcrypt($data['password']);

            if (empty($data['name']))
                myResult()->stop(ResultTool::CODE_PARAM_LOST,'昵称不可为空');

            if (getStrLength($data['name']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'昵称过长');

            if (getStrLength($data['name']) > 20)
                myResult()->stop(ResultTool::CODE_PARAM_WRONG,'昵称过长');

            $data['organize_id'] = $this->organize->id;
        }

        if (isset($data['role'])){
            $data['role_id'] = null;
            $role_log = $this->organize->role()->where('id',$data['role'])->first();
            if ($role_log) {
                $data['role_id'] = $data['role'];
            }
            unset($data['role']);
        }

        return $data;
    }
}
